package com.ibuonline.b2b.admin.action;

import java.util.List;

import com.dimeng.common.base.BaseAction;
import com.dimeng.search.support.SearchPage;
import com.ibuonline.b2b.entity.Company;
import com.ibuonline.b2b.entity.Product;
import com.ibuonline.b2b.member.service.CompanyService;
import com.ibuonline.b2b.product.service.ProductService;
import com.ibuonline.b2b.search.CompanySearcher;
import com.ibuonline.b2b.search.ProductSearcher;

public class WasteIndexAction extends BaseAction {
	
	private static final long serialVersionUID = -7641707618391860360L;
	
	private ProductService productService;
	private CompanyService companyService;
	private ProductSearcher productSearcher;
	private CompanySearcher companySearcher;
	private String keyword;
	private List<Product> productList;
	private List<Long> productIds;
	private List<Company> companyList;
	private List<Integer> companyIds;
	private long totalCount;
	private String infoType;
	
	public String queryIndex() {
		if ("product".equals(infoType)) {
			queryProduct();
		} else if ("company".equals(infoType)) {
			queryCompany();
		}
		return SUCCESS;
	}
	
	private void queryProduct() {
		SearchPage<Product>  sp = new SearchPage<Product>();
		sp.setKeyword(keyword);
		sp = productService.search(sp);
		totalCount = sp.getTotalCount();
		productList = sp.getResult();
	}
	
	private void queryCompany() {
		SearchPage<Company>  sp = new SearchPage<Company>();
		sp.setKeyword(keyword);
		sp = companyService.search(sp);
		totalCount = sp.getTotalCount();
		companyList = sp.getResult();
	}
	
	public String deleteIndex() {
		if ("product".equals(infoType)) {
			if (productIds != null && productIds.size() > 0) {
				for (Long productId : productIds) {
					Product product = new Product();
					product.setId(productId);
					productSearcher.delete(product);
				}
			}
		} else if ("company".equals(infoType)) {
			if (companyIds != null && companyIds.size() > 0) {
				for (Integer companyId : companyIds) {
					Company company = new Company();
					company.setCompanyId(companyId);
					companySearcher.delete(company);
				}
			}
		}
		return queryIndex();
	}

	// getter and setter
	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public ProductService getProductService() {
		return productService;
	}

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	public List<Product> getProductList() {
		return productList;
	}

	public void setProductList(List<Product> productList) {
		this.productList = productList;
	}

	public List<Long> getProductIds() {
		return productIds;
	}

	public void setProductIds(List<Long> productIds) {
		this.productIds = productIds;
	}

	public ProductSearcher getProductSearcher() {
		return productSearcher;
	}

	public void setProductSearcher(ProductSearcher productSearcher) {
		this.productSearcher = productSearcher;
	}

	public long getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(long totalCount) {
		this.totalCount = totalCount;
	}

	public String getInfoType() {
		return infoType;
	}

	public void setInfoType(String infoType) {
		this.infoType = infoType;
	}

	public CompanyService getCompanyService() {
		return companyService;
	}

	public void setCompanyService(CompanyService companyService) {
		this.companyService = companyService;
	}

	public CompanySearcher getCompanySearcher() {
		return companySearcher;
	}

	public void setCompanySearcher(CompanySearcher companySearcher) {
		this.companySearcher = companySearcher;
	}

	public List<Integer> getCompanyIds() {
		return companyIds;
	}

	public void setCompanyIds(List<Integer> companyIds) {
		this.companyIds = companyIds;
	}

	public List<Company> getCompanyList() {
		return companyList;
	}

	public void setCompanyList(List<Company> companyList) {
		this.companyList = companyList;
	}
}
